<?php
/**
 * CourseController.class.php
 * 课程控制器
 *
 * @author  王中艺
 * @date    2016-11-09
 */

namespace Wechat\Controller;

class CourseController extends BaseController {

    /**
     * 课程列表获取
     * @param   type
     * @param   page
     * @return  json
     */
    function index(){
        $buyType   = $this->request('type', false);
        $cate       = $this->request('cate', false);
        $page       = $this->request('page', 1);

        $list   = D('Course')->getList(C('user.id'), $buyType, $cate, $page);
        $this->outOInfo(['list' => $list, 'page' => $page], 1);
    }

    /**
     * 课程购买-现金
     * @param   course_id
     * @return  json
     */
    function buyByMoney(){
        $this->unEmptyParam(array('course_id'));
        $courseId   = $this->iInfo['course_id'];

        $dCourse    = D('Course');
        $moneyInfo  = $dCourse->buyByMoney($courseId, C('user.id'));
        if(!$moneyInfo)
            $this->outOInfo($dCourse->errMsg, 0);

        //支付金额为0时，自动调用支付回调
        if($moneyInfo['amount'] == 0){
            $dCourse->paySuccess($moneyInfo);
            $this->outOInfo('免费购买成功!', 302);
        }

        //生成JSsdk支付信息
        $wxPay  = new \Wechat\Util\WxPay();
        $payInfo    = $wxPay->createJsPay(
            $moneyInfo['id'],
            '正心源课程',
            $moneyInfo['trade_no'],
            $moneyInfo['amount'] * 100,
            C('user.open_id')
        );
        if(!$payInfo)
            $this->outOInfo('JSsdk支付信息生成失败!', 0);

        $this->outOInfo($payInfo);
    }

    /**
     * 课程购买 - 积分
     * @param   course_id
     * @return  json    
     */
    function buyByScore(){
        $this->unEmptyParam(array('course_id'));

        $courseId   = $this->iInfo['course_id'];
        $dCourse    = D('Course');
        $result     = $dCourse->buyByScore($courseId, C('user.id'));
        if($result === false)
            $this->outOInfo($dCourse->errMsg, 0);

        $this->outOInfo('Oh yes!');
    }

    /**
     * 课程章节信息获取
     * @param   course_id
     * @return  json 课程信息
     */
    function chapters(){
        $this->unEmptyParam(array('course_id'));

        $courseId   = $this->iInfo['course_id'];
        $isTry  = $this->iInfo['is_try'] == 0 ? 0 : 1;
        $dCourse    = D('Course');
        $chapters     = $dCourse->chapters(C('user.id'), $courseId);
        $courseInfo   = $dCourse->detail($courseId, C('user.id'));
        if($chapters === false || $courseInfo === false){
            $this->outOInfo($dCourse->errMsg, 0);
        }

        $chapterList    = D('Course')->chapters(C('user.id'), $courseId);
        $courseInfo     = D('Course')->detail($courseId, C('user.id'));
        if($chapterList === false || $courseInfo === false)
            $this->outOInfo(D('Course')->errMsg, 0);

        $leaveMsgList   = D('LeaveMsg')->getList(1, $courseId);
        if($leaveMsg === false)
            $leaveMsg   = [];

        $this->outOInfo([
            // 'list' => $chapters, 
            'chapter_list'  => $chapterList, 
            'course_info'   => $courseInfo, 
            'leavemsg_list' => $leaveMsgList
        ]);
    }

    /**
     * 课程详情信息获取
     * @param   course_id
     * @return  json
     */
    function detail(){
        $this->unEmptyParam(array('course_id'));

        $courseId   = $this->iInfo['course_id'];
        $courseInfo     = D('Course')->detail($courseId, C('user.id'));
        if($courseInfo === false)
            $this->outOInfo(D('Course')->errMsg, 0);
        
        $leaveMsgList   = D('LeaveMsg')->getList(1, $courseId);
        if($leaveMsg === false)
            $this->outOInfo(D('LeaveMsg')->errMsg, 0);
        
        $this->outOInfo(['course_info' => $courseInfo, 'leavemsg_list' => $leaveMsgList]);
    }

    /**
     * 免费课程列表获取
     * @return   json
     */
    function freeList(){
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;
        $list   = D('Course')->freeList($page);
        $this->outOInfo(array('list' => $list));
    }

    /**
     * 记录观看事件
     * @param   $course_id 课程ID
     * @param   $chapter_id 章节ID
     */
    function addWatchLog(){
        $this->unEmptyParam(array('course_id', 'chapter_id'));
        $ret    = D('Course')->addWatchLog(
            $this->iInfo['course_id'], 
            $this->iInfo['chapter_id'],
            C('user.id')
        );

        if($ret === false)
            $this->outOInfo(D('Course')->errMsg, 0);

        $this->outOInfo('记录成功!');
    }

    /**
     * 获取最近观看课程
     */
    function getLastWatch(){
        $userId     = C('user.id');
        $courseInfo     = D('Course')->getLastWatch($userId);
        if($courseInfo === false)
            $this->outOInfo('信息查询失败!', 0);
        $this->outOInfo($courseInfo);
    }
}
